# A simple generator function  
def my_gen():  
    n = 1  
    print('This is printed first')  
    # Generator function contains yield statements  
    yield n  
  
    n += 1  
    print('This is printed second')  
    yield n  
  
    n += 1  
    print('This is printed at last')  
    yield n

a =  my_gen()
print a.next()
print a.next()
print a.next()
# print a.next()

for item in my_gen():  
    print(item)



def rev_str(my_str):
    length = len(my_str)  
    for i in range(length - 1,-1,-1):  
        yield my_str[i]

for char in rev_str("hello"):  
     print(char)


"""
list comprehension
"""

my_list = [1, 3, 6, 10]

print [x**2 for x in my_list]
print (x**2 for x in my_list)



class PowTwo:  
    def __init__(self, max = 0):  
        self.max = max  
  
    def __iter__(self):  
        self.n = 0  
        return self  
  
    def __next__(self):  
        if self.n > self.max:  
            raise StopIteration  
  
        result = 2 ** self.n  
        self.n += 1  
        return result


def PowTwoGen(max = 0):  
    n = 0  
    while n < max:  
        yield 2 ** n  
        n += 1 



